<?php
/**
 * Created by PhpStorm.
 * User: chen
 * Date: 18-5-26
 * Time: 下午2:57
 */

namespace App\Model\Common;

use Validator;

class Module extends BasePaginateModel
{
    public $table = 'nosplit_module';
    public $primaryKey = 'id';
    protected $hidden = ['created_at', 'updated_at'];

    public $errors = null;


    public function isValid($params = null){
        if(empty($params)){
            $params = $this->toArray();
        }
        $except = '';
        if(!empty($this->id)){
            $except = ',id,'.$this->id;
        }
        $valid = Validator::make($params, [
            'name'             => 'required|unique:'.$this->table. $except,
        ], [
            'name.required' => '模块名称必填',
            'name.unique' => '模块名称已存在',
        ]);
        $this->errors = $valid->messages();
        return !$valid->fails();
    }

    public function authorities(){
        return $this->hasMany('App\Model\Common\Authority', 'module_id');
    }
}